﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CONCURSO.EN;
using MJM.CBD.Conn;

namespace MJM.CONCURSO.AD
{
    public static class ParticipanteAD
    {
        private static string FechaFormato(string fecha)
        {
            return fecha.Substring(6, 2) + "/" + fecha.Substring(4, 2) + "/" + fecha.Substring(0, 4);
        }

        public static int Insertar(ParticipanteEN obj)
        {
            int Codigo = 0;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_PARCON_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            try
            {
                cmd.Parameters.Add(new SqlParameter("@vPARCON_chFECRES", obj.Fecha));
                cmd.Parameters.Add(new SqlParameter("@vPARCON_chHORRES", obj.Hora));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IDConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPARCON_inRESCOR", obj.Activo));
                cmd.Parameters.Add(new SqlParameter("@vPAD_P_chIDPER", obj.IDPersona));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return Codigo;
        }

        public static List<ParticipanteEN> Listar(ParticipanteEN obj)
        {
            List<ParticipanteEN> lst = new List<ParticipanteEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_PARCON_Listar", conn);
            SqlDataReader dr;
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IDConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPAD_P_chIDPER", obj.IDPersona));
                cmd.Parameters.Add(new SqlParameter("@vPARCON_inRESCOR", obj.Activo));
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    var objParticipante = new ParticipanteEN();
                    objParticipante.ID = dr.GetInt32(dr.GetOrdinal("PARCON_P_inID"));
                    objParticipante.IDConcurso = dr.GetInt32(dr.GetOrdinal("CONVEC_P_inID"));
                    objParticipante.IDPersona = dr.GetString(dr.GetOrdinal("PAD_P_chIDPER"));
                    objParticipante.Fecha = FechaFormato(dr.GetString(dr.GetOrdinal("PARCON_chFECRES")));
                    objParticipante.Hora = dr.GetString(dr.GetOrdinal("PARCON_chHORRES"));
                    objParticipante.Activo = dr.GetInt32(dr.GetOrdinal("PARCON_inRESCOR"));
                    objParticipante.Nombre = dr.GetString(dr.GetOrdinal("PAD_chNOMCOM"));
                    objParticipante.DNI = dr.GetString(dr.GetOrdinal("PAD_chNRODOC"));                    
                    lst.Add(objParticipante);
                }

                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
                return lst;
        }
    }
}
